/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spring;

import java.awt.*;



/**
 *
 * @author spring
 */
public class PlotPanel1HOLD extends javax.swing.JPanel{
    double[][] locMatrix = null;
    public PlotPanel1HOLD(){
    }
    public void setMatrix(double[][] M){
        locMatrix=M;
    }
    public void paint(Graphics g){
       Dimension size = getSize();
         // TODO add your handling code here:
      super.paint(g);
      
      if (locMatrix == null){
      return;
      }
      
      int h = this.getHeight();
      int w = this.getWidth();
      int s = locMatrix.length;
      char[] NODES = new char[52];
      for (int i=0;i<26;i++) {
        NODES[i]=(char)(i+65);
        NODES[i+26]=(char)(i+97);
        }
     /* int it;
      double[][] loc = new double[s][2];
      double[][] plotMatrix = new double[s][s];
      double[][] grad = new double[s][2];
      double lasterror=0.0, totalerror=0.0, errorcomp=0.0;
      double rate = 0.001;//new Double(Rate.getText()).doubleValue();
      double d=0.0;
      boolean first = true;
     
      //System.out.println("Size is "+s+"\n");
      g.setColor(new java.awt.Color(0,0,255));
      g.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 20));
      //set random locations for the feed entries 
      for (int i=0;i<s;i++){        
        loc[i][0]=Math.random()*.5+.25;
        loc[i][1]=Math.random()*.5+.25;
        g.drawChars(NODES, i%52, 1, (int)(loc[i][0]*w), (int)(loc[i][1]*h));
        //GC.fillArc((int)(loc[i][0]*w), (int)(loc[i][1]*h), 25, 25, 0, 360);
      }
      //iterate upto 1000 times to place the entries
      for(it=0;it<1000;it++){
          // find plot distances
          for (int i=0;i<s;i++)
              for (int j=0;j<s;j++){        
               // if (PDist.isSelected()) d = getPearsonCorrelation(loc[k],loc[j]); 
                //if (EDist.isSelected()) d = getSimDistance(loc[k],loc[j]);
                plotMatrix[i][j]=getSimDistance(loc[i],loc[j]);
              }//end find plot distances
          for (int i=0;i<s;i++){        
            grad[i][0]=0.0;
            grad[i][1]=0.0;
          }
          // find error in plots
          totalerror = 0.0;
          for (int j=0;j<s;j++)
              for (int k=0;k<s;k++){
                  if (j==k||distMatrix[k][j]==0.0) continue;
                  if (distMatrix[k][j]==0.0) System.out.println("Whoops zero dist");
                  if (plotMatrix[k][j]==0.0) continue;//System.out.println("Whoops zero plot");
                  errorcomp=(plotMatrix[k][j]-distMatrix[k][j])/distMatrix[k][j];
                  grad[j][0]+=((loc[j][0]-loc[k][0])/plotMatrix[k][j])*errorcomp;
                  grad[j][1]+=((loc[j][1]-loc[k][1])/plotMatrix[k][j])*errorcomp;
                  totalerror+=Math.abs(errorcomp);
              }

          if (!first && lasterror<totalerror) break;
          first = false;
          lasterror=totalerror;
      //System.out.println("Total Iterations "+it+": Total error = "+totalerror);
      g.setColor(new java.awt.Color((it/4),0,255-(it/4)));
      for (int j=0;j<s;j++){
          loc[j][0]-=rate*grad[j][0];
          loc[j][1]-=rate*grad[j][1];
          if (loc[j][0]<.01)loc[j][0]=.01;
          if (loc[j][0]>.98)loc[j][0]=.98;
          if (loc[j][1]<.03)loc[j][1]=.03;
          if (loc[j][1]>.99)loc[j][1]=.99;
          g.drawChars(NODES, j%52, 1, (int)(loc[j][0]*w), (int)(loc[j][1]*h));
          // GC.fillArc((int)(loc[j][0]*w), (int)(loc[j][1]*h), 20, 20, 0, 360);
         }
      }// end iteration for 1000
      // Disp.append("Total Iterations "+it+": Total error = "+totalerror);
      //Iter.setText(" "+it);
      //Error.setText(" "+totalerror);*/
       for (int j=0;j<s;j++){
           g.setColor(new java.awt.Color(255,255,255));
           g.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 25));
           g.drawChars(NODES, j%52, 1, (int)(locMatrix[j][0]*w), (int)(locMatrix[j][1]*h));
           g.setColor(new java.awt.Color(0,0,0));
           g.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 13));
         // if (feeds)GC.drawString(SFeed[j].getTitle(), (int)(loc[j][0]*w), (int)(loc[j][1]*h));
         // if (entries)GC.drawString(EntryTitles[j], (int)(loc[j][0]*w), (int)(loc[j][1]*h));
         }
    }

    private double getSimDistance(double[] scores1,double[] scores2){
    double sum = 0.0;
    for (int i=0;i<scores1.length; i++)
    {
        sum += Math.pow((scores2[i]-scores1[i]), 2.0);
        
    }
    double distance = Math.sqrt(sum);
    return 1.0-(1/(1+distance));
}
    
    
}
